package cn.xyf.eduservice.controller;


import cn.xyf.commonutils.R;
import cn.xyf.eduservice.domain.EduTeacher;
import cn.xyf.eduservice.domain.vo.TeacherQuery;
import cn.xyf.eduservice.service.EduTeacherService;
import cn.xyf.servicebase.handler.MyException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author xuyinfeng
 * @since 2021-04-01
 */

@Api(tags = "教师模块")
@RestController // 让spring创建对象并返回json数据
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation("查询所有")
    @GetMapping("/findAll")
    public R findAllTeacher() {
        final List<EduTeacher> list = eduTeacherService.list();
        return R.ok().data("data", list);
    }

    @ApiOperation("分页查询")
    @GetMapping("/find/{current}/{size}")
    public R pageTeacher(
            @ApiParam(name = "current", value = "页码", required = true)
            @PathVariable long current,
            @ApiParam(name = "size", value = "每页显示的个数", required = true)
            @PathVariable long size) {
        Page<EduTeacher> page = new Page<>(current, size);
        eduTeacherService.page(page, null);

        final long total = page.getTotal();
        final List<EduTeacher> records = page.getRecords(); // 获取数据集合

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total", total);
        hashMap.put("rows", records);

        return R.ok().data(hashMap);
    }

    @ApiOperation("带条件的分页查询")
    @PostMapping("/findCondition/{current}/{size}")
    public R pageTeacherCondition(
            @ApiParam(name = "current", value = "页码", required = true)
            @PathVariable long current,
            @ApiParam(name = "size", value = "每页显示的个数", required = true)
            @PathVariable long size,
            @ApiParam(name = "teacherQuery", value = "讲师对象", required = true)
            // 把传递过来的json数据封装到对象中
            @RequestBody(required = false) TeacherQuery teacherQuery) {

        final Page<EduTeacher> page = new Page<>(current, size);
        final QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        if (teacherQuery != null) {
            final String name = teacherQuery.getName();
            final Integer level = teacherQuery.getLevel();
            final String start = teacherQuery.getStart();
            final String end = teacherQuery.getEnd();

            if (StringUtils.hasLength(name)) {
                wrapper.like("name", name);
            }
            if (level != null) {
                wrapper.eq("level", level);
            }
            if (StringUtils.hasLength(start)) {
                wrapper.ge("gmt_create", start);
            }
            if (StringUtils.hasLength(end)) {
                wrapper.le("gmt_create", end);
            }
        }

        // 根据创建时间降序排序 DESC: 降序, ASC: 升序。
        wrapper.orderByDesc("gmt_create");
        eduTeacherService.page(page, wrapper);

        final long total = page.getTotal();
        final List<EduTeacher> records = page.getRecords();
        final HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total", total);
        hashMap.put("rows", records);
        return R.ok().data(hashMap);
    }

    @ApiOperation("删除教师")
    @DeleteMapping("/delete/{id}")
    public R deleteTeacher(
            @ApiParam(name = "id", value = "教师id", required = true)
            @PathVariable String id) {
        final boolean bool = eduTeacherService.removeById(id);
        return bool ? R.ok() : R.error();
    }

    @ApiOperation("添加教师")
    @PostMapping("/add")
    public R addTeacher(
            @ApiParam(name = "eduTeacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher) {
        final boolean b = eduTeacherService.save(eduTeacher);
        return b ? R.ok() : R.error();
    }

    @ApiOperation("根据ID查找教师")
    @GetMapping("/find/{id}")
    public R findOneById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        System.out.println(eduTeacher + "----" + id);
        return R.ok().data("data", eduTeacher);
    }

    @ApiOperation("修改教师")
    @PostMapping("/update")
    public R updateTeacher(
            @ApiParam(name = "eduTeacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher) {
//        try {
//            int i = 10 / 0;
//        } catch (Exception e) {
//            throw new MyException(201, "自定义异常");
//        }

        final boolean b = eduTeacherService.updateById(eduTeacher);
        return b ? R.ok() : R.error();
    }
}

